package com.xuecheng.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.system.dto.DictionaryDTO;
import com.xuecheng.system.convert.DictionaryConvert;
import com.xuecheng.system.entity.Dictionary;
import com.xuecheng.system.mapper.DictionaryMapper;
import com.xuecheng.system.service.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 数据字典 服务实现类
 *
 * @author itcast
 */
@Slf4j
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Override
    public List<DictionaryDTO> queryAll() {
        List<Dictionary> dictionaries = dictionaryMapper.selectList(null);

        List<DictionaryDTO> dtoList = DictionaryConvert.INSTANCE.entitys2dtos(dictionaries);

        return dtoList;
    }

    @Override
    public DictionaryDTO getDicByCode(String code) {

        LambdaQueryWrapper<Dictionary> wrapper = Wrappers.lambdaQuery(Dictionary.class);
        wrapper.eq(Dictionary::getCode,code);
        Dictionary dictionary = dictionaryMapper.selectOne(wrapper);

        DictionaryDTO dictionaryDTO = new DictionaryDTO();
        if(dictionary != null){
            dictionaryDTO = DictionaryConvert.INSTANCE.entity2dto(dictionary);
        }

        return dictionaryDTO;
    }
}
